//
//  YWDailyReportCountCell.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/6/6.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWDailyReportCountCell.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWDailyReport.h"
#import "YWBuilding.h"
#import "YWBuildingManager.h"

@interface YWDailyReportCountCell()
@property (nonatomic, strong) UILabel * buildingLb;
@property (nonatomic, strong) UILabel * repairLb;
@property (nonatomic, strong) UILabel * repairCountLb;
@property (nonatomic, strong) UILabel * detectLb;
@property (nonatomic, strong) UILabel * detectCountLb;
@property (nonatomic, strong) UILabel * upkeepLb;
@property (nonatomic, strong) UILabel * upkeepCountLb;
@property (nonatomic, strong) UILabel * keepWatchLb;
@property (nonatomic, strong) UILabel * keepWatchCountLb;
@property (nonatomic, strong) UILabel * inspectLb;
@property (nonatomic, strong) UILabel * inspectCountLb;
@end

@implementation YWDailyReportCountCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.layer.shouldRasterize = YES;
        self.layer.rasterizationScale = [UIScreen mainScreen].scale;
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setDailyReportArray:(NSMutableArray *)dailyReportArray{
    _dailyReportArray = dailyReportArray;
    YWDailyReport * dailyReport= _dailyReportArray[0];
    YWBuilding * building = [[YWBuildingManager shareManager] buildingWithId:dailyReport.buildingId];
    _buildingLb.text = building.buildingName?:@"无";
    [_dailyReportArray enumerateObjectsUsingBlock:^(YWDailyReport*  _Nonnull dailyReport, NSUInteger idx, BOOL * _Nonnull stop) {
        if (dailyReport.maintenanceTypeId == YWMaintenanceTypePatrol) {
            NSString * inspectionCount = [NSString stringWithFormat:@"%d%%",(dailyReport.finishCount/dailyReport.total)*100];
            _inspectCountLb.text = inspectionCount;
        }else if (dailyReport.maintenanceTypeId==YWMaintenanceTypeDetection){
            NSString * detectCount = [NSString stringWithFormat:@"%d%%",(dailyReport.finishCount/dailyReport.total)*100];
            _detectCountLb.text = detectCount;
        }else if (dailyReport.maintenanceTypeId==YWMaintenanceTypeUpkeep){
            NSString * detectCount = [NSString stringWithFormat:@"%d%%",(dailyReport.finishCount/dailyReport.total)*100];
            _upkeepCountLb.text = detectCount;
        }else if (dailyReport.maintenanceTypeId== YWMaintenanceTypeTrouble){
            NSString * repairCount = [NSString stringWithFormat:@"%d%%",(dailyReport.finishCount/dailyReport.total)*100];
            _repairCountLb.text = repairCount;
        }else if (dailyReport.maintenanceTypeId == YWMaintenanceTypeKeepWatch){
            NSString * keepWatchCount = [NSString stringWithFormat:@"%d%%",(dailyReport.finishCount/dailyReport.total)*100];
            _keepWatchCountLb.text = keepWatchCount;
        }
    }];
}

- (void)setUpSubviews{
    UIView *superView = self.contentView;
    [self.buildingLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).offset(kMarginW);
        make.left.mas_equalTo(superView).offset(2*kMarginW);
        make.right.mas_equalTo(superView).offset(-kMarginW);
    }];
    [self.repairCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buildingLb.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(superView).offset(2*kMarginW);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
        make.height.mas_equalTo(kLabelH);
    }];
    [self.repairLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_repairCountLb.mas_bottom).offset(5);
        make.left.mas_equalTo(superView).offset(2*kMarginW);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
        make.bottom.mas_equalTo(superView).offset(-kMarginW);
    }];
    [self.detectCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buildingLb.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(_repairCountLb.mas_right);
        make.height.mas_equalTo(kLabelH);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
    }];
    [self.detectLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_detectCountLb.mas_bottom).offset(5);
        make.left.mas_equalTo(_repairLb.mas_right);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
        make.bottom.mas_equalTo(superView).offset(-kMarginW);
    }];
    [self.upkeepCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buildingLb.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(_detectCountLb.mas_right);
        make.height.mas_equalTo(kLabelH);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
    }];
    [self.upkeepLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_upkeepCountLb.mas_bottom).offset(5);
        make.left.mas_equalTo(_detectLb.mas_right);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
        make.bottom.mas_equalTo(superView).offset(-kMarginW);
    }];
    [self.keepWatchCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buildingLb.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(_upkeepCountLb.mas_right);
        make.height.mas_equalTo(kLabelH);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
    }];
    [self.keepWatchLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_keepWatchCountLb.mas_bottom).offset(5);
        make.left.mas_equalTo(_upkeepLb.mas_right);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
        make.bottom.mas_equalTo(superView).offset(-kMarginW);
    }];
    [self.inspectCountLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buildingLb.mas_bottom).offset(kMarginW);
        make.left.mas_equalTo(_keepWatchCountLb.mas_right);
        make.height.mas_equalTo(kLabelH);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
    }];
    [self.inspectLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_inspectCountLb.mas_bottom).offset(5);
        make.left.mas_equalTo(_keepWatchLb.mas_right);
        make.width.mas_equalTo((SCREEN_WIDTH-20)/5);
        make.bottom.mas_equalTo(superView).offset(-kMarginW);
    }];
}

- (UILabel *)buildingLb{
    if (!_buildingLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = KTitleFont;
        label.textColor = [UIColor customTextGrayColor];
        _buildingLb = label;
        [self.contentView addSubview:_buildingLb];
    }
    return _buildingLb;
}

- (UILabel *)repairLb{
    if (!_repairLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = NSLocalizedString(@"trouble", nil);
        label.textColor = [UIColor customTextGrayColor];
        _repairLb = label;
        [self.contentView addSubview:_repairLb];
    }
    return _repairLb;
}

- (UILabel *)repairCountLb{
    if (!_repairCountLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _repairCountLb = label;
        [self.contentView addSubview:_repairCountLb];
    }
    return _repairCountLb;
}

- (UILabel *)detectLb{
    if (!_detectLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = NSLocalizedString(@"detection", nil);
        label.textColor = [UIColor customTextGrayColor];
        _detectLb = label;
        [self.contentView addSubview:_detectLb];
    }
    return _detectLb;
}

- (UILabel *)detectCountLb{
    if (!_detectCountLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _detectCountLb = label;
        [self.contentView addSubview:_detectCountLb];
    }
    return _detectCountLb;
}

- (UILabel *)upkeepLb{
    if (!_upkeepLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = NSLocalizedString(@"upkeep", nil);
        label.textColor = [UIColor customTextGrayColor];
        _upkeepLb = label;
        [self.contentView addSubview:_upkeepLb];
    }
    return _upkeepLb;
}

- (UILabel *)upkeepCountLb{
    if (!_upkeepCountLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _upkeepCountLb = label;
        [self.contentView addSubview:_upkeepCountLb];
    }
    return _upkeepCountLb;
}


- (UILabel *)keepWatchLb{
    if (!_keepWatchLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = NSLocalizedString(@"keepWatch", nil);
        label.textColor = [UIColor customTextGrayColor];
        _keepWatchLb = label;
        [self.contentView addSubview:_keepWatchLb];
    }
    return _keepWatchLb;
}

- (UILabel *)keepWatchCountLb{
    if (!_keepWatchCountLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _keepWatchCountLb = label;
        [self.contentView addSubview:_keepWatchCountLb];
    }
    return _keepWatchCountLb;
}


- (UILabel *)inspectLb{
    if (!_inspectLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.text = NSLocalizedString(@"inspection", nil);
        label.textColor = [UIColor customTextGrayColor];
        _inspectLb = label;
        [self.contentView addSubview:_inspectLb];
    }
    return _inspectLb;
}

- (UILabel *)inspectCountLb{
    if (!_inspectCountLb) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor blackColor];
        _inspectCountLb = label;
        [self.contentView addSubview:_inspectCountLb];
        _inspectCountLb = label;
    }
    return _inspectCountLb;
}


@end
